
https://github.com/mikeal/request
var url = "",
    express = require('express');

var app = express(),
    request = require('request'),
    assert = require("assert"),
    cheerio = require('cheerio');

describe('register page (/register)',function(){

    before(function(done){
        url = 'http://localhost:3010';
        done();
    });

    it('GET should return 200',function(done){
        request(url + "/register", function(error,response,body){
            assert.equal(response.statusCode, 200);
            done();
        });
    });

    it('GET should show a register form',function(done){
        request(url + "/register", function(error,response,body){
            var $ = cheerio.load(body);
            assert.equal($('form').attr('action'),"/register");
            done();
        });
    });

    it('POST should refuse bad submissions', function(done){

        request.post(url + "/register",{form:{fail: 'fail'}}, function(error,response,body){
            var $ = cheerio.load(body);
            assert.notEqual($('.field.error').length,0);
            done();
        })

    });

    it('POST should refuse partial submissions', function(done){
        request.post(url + "/register",{form:{firstname:'Eric', lastname: 'Boyer'}}, function(error,response,body){
            var $ = cheerio.load(body);
            assert.notEqual($('.field.error').length,0);
            done();
        })
    });

    it('POST should keep values on partial submissions', function(done){
        request.post(url + "/register",{form:{lastname: 'Boyer'}}, function(error,response,body){
            var $ = cheerio.load(body);
            assert.equal($('#id_lastname').attr('value'),"Boyer");
            done();
        })
    });

    it('POST should refuse invalid emails', function(done){
        request.post(url + "/register",{form:{email: 'badEmail'}}, function(error,response,body){
            var $ = cheerio.load(body);
            var emailInput = $('#id_email');
            var divEmail = $(emailInput).parent();
            assert.ok(divEmail.hasClass('error'));
            done();
        })
    });

    it('POST should accept complete submissions',function(done){
        request.post(url + "/register",{form:{lastname: 'test',firstname : 'test',pseudo : "pseudoTest",
            email : 'test@test.com', password : 'password', confirm: 'password'}}, function(error,response,body){
            assert.equal(response.statusCode, 302);
            assert.equal(body,'Moved Temporarily. Redirecting to /login');
            done();
        })
    });

    it('POST should refuse duplicate emails',function(done){
        request.post(url + "/register",{form:{lastname: 'test',firstname : 'test',pseudo : "pseudoTest2",
            email : 'test@test.com', password : 'password', confirm: 'password'}},
                function(error,response,body){
                    var $ = cheerio.load(body);
                    assert.notEqual($('.globalError').length,0);
                    done();
                })
    });

    it('POST should refuse duplicate pseudo',function(done){
        request.post(url + "/register",{form:{lastname: 'test',firstname : 'test',pseudo : "pseudoTest",
                email : 'test2@test.com', password : 'password', confirm: 'password'}},
            function(error,response,body){
                var $ = cheerio.load(body);
                assert.notEqual($('.globalError').length,0);
                done();
            })
    });

});